﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;

namespace TanashCMS.Business.Task
{
    public class UrlRewriterTask
    {
        public string ConvertToXPath(Uri uri, CMSObjectType type)
        {
            string contentXPath = "";

            // split url path into parts
            string[] pitems = splitUrl(uri.AbsoluteUri);

            // a product should be in the format /category/category/product
            List<string> contentPathList = new List<string>(); // this is just to make resizing the path array easier
            string[] contentPath = new string[pitems.Length - 1];
            for (int i = 0; i < pitems.Length; i++)
                if (pitems[i].Trim() != string.Empty) contentPathList.Add(Path.GetFileNameWithoutExtension(pitems[i].Trim()));

            contentPath = contentPathList.ToArray();

            if (type == CMSObjectType.WebPage)
                return getWebPagePath(contentPath);
            else if (type == CMSObjectType.BlogPost)
                return getBlostPostPath(contentPath);
            else if (type == CMSObjectType.Category)
                return getCategoryPath(contentPath);
            else if (type == CMSObjectType.Product)
                return getProductPath(contentPath);

            return "";
        }

        private string getBlostPostPath(string[] contentPath)
        {
            string contentXPath = "";
            foreach (string p in contentPath)
            {
                if (p.Length == 0)
                    continue;

                contentXPath += string.Format("/blogpost[@key='{0}']", p);
            }

            return "/website" + contentXPath;
        }

        public static string[] splitUrl(string url)
        {
            // split url path into parts
            string urlPath = HttpUtility.UrlPathEncode(url).Substring(url.IndexOf("://") + 3);
            urlPath = urlPath.Substring(urlPath.IndexOf("/") + 1);
            return urlPath.Split('/');
        }

        private string getWebPagePath(string[] contentPath)
        {
            string contentXPath = "";
            foreach (string p in contentPath)
            {
                if (p.Length == 0)
                    continue;

                contentXPath += string.Format("/webpage[@key='{0}']", p);
            }

            return "/website" + contentXPath;
        }

        private string getCategoryPath(string[] contentPath)
        {
            string contentXPath = "";
            foreach (string p in contentPath)
            {
                if (p.Length == 0)
                    continue;

                contentXPath += string.Format("/category[@key='{0}']", p);
            }

            return "/website/inventory" + contentXPath;
        }

        private string getProductPath(string[] contentPath)
        {
            string contentXPath = "";
            int c = 1;
            foreach (string p in contentPath)
            {
                if (p.Length == 0)
                    continue;

                if (contentPath.Length == 1)
                    contentXPath += string.Format("/category/product[@key='{0}']", p);
                else if (c == contentPath.Length)
                    contentXPath += string.Format("/product[@key='{0}']", p);
                else
                    contentXPath += string.Format("/category[@key='{0}']", p);

                c++;
            }

            return "/website/inventory" + contentXPath;
        }
    }
}
